// pages/ticket/ticket-money/ticket-money.js
var httpUtil = require('../../../utils/httpUtil.js')
var uiUtil = require('../../../utils/uiUtil.js')
const app = getApp()
const MAX_PAGE_COUNT = 30
var that

Page({

  /**
   * 页面的初始数据
   */
  data: {
    cardDetail:null,
    detailLists:{
      totalCount: null,
      pageCount: null,
      pageNo: "0",
      pageSize: "30",
      balance: null,
      detailList: []
    },
    requestGoodsTask: null, //请求task
    isLoadingMore: false,
    enableLoadMore: true
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    that = this;
    that.getTicketLists(options.id);
    that.ticketDetails(options.id);
    that.setData({
      options: options
    })
  },

  /**
   * 获取会员卡详细信息
   */
  ticketDetails(mCardId) {
    httpUtil.PostRequest("detail", {
      mCardId: mCardId
    }, {
        success(res) {
          that.setData({
            cardDetail: res
          })
        },
        fail(code, res) {
          uiUtil.showToast(res);
        },
        complete() { }
      })
  },

  /**
   * 获取余额明细列表
   */
  getTicketLists(mCardId) {
    let pageNo = (parseInt(that.data.detailLists.pageNo) + 1).toString();
    let pageSize = that.data.detailLists.pageSize;
    let detailList = that.data.detailLists.detailList;
    let totalCount = that.data.detailLists.totalCount;

    var lastRequestTask = that.data.requestGoodsTask;
    if (lastRequestTask) {
      try {
        lastRequestTask.abort()
      } catch (e) { }
    }

    var curPage = that.data.detailLists.pageNo
    if (curPage != 0 && (!that.data.enableLoadMore || that.data.isLoadingMore)) { //不可加载||正在加载
      return;
    }

    var requestTask =httpUtil.PostRequest("card_detail_list", {
      mCardId: mCardId,
      pageNo: pageNo,
      pageSize: pageSize
    }, {
      success(res) {
        if (0 == that.data.detailLists.totalCount) {
          res.detailList = res.detailList || []
        } else {
          res.detailList = detailList.concat(res.detailList || []);
        }
        that.setData({
          detailLists: res,
          enableLoadMore: res.detailList.length < res.totalCount
        })
      },
      fail(code, res) {
        that.setData({
          enableLoadMore: false
        })
        uiUtil.showToast(res);
      },
      complete() {
        that.setData({
          isLoadingMore: false
        })
      }
    })
    that.setData({
      requestGoodsTask: requestTask
    })
  },

  /**
   * 滑动到底部
   */
  onReachBottom: function () {
    this.getTicketLists(that.data.options.id);
  },

  /**
   * 跳转会员卡余额明细详情页面
   */
  cardDetail:function(e){
    let orderid = e.currentTarget.dataset.orderid;
    let businesstype = e.currentTarget.dataset.businesstype;
    var url;
    if (businesstype==1){
      url = "/pages/details/top-ups/top-ups?orderId=" + orderid;
    } else{
      url = "/pages/details/consume/consume?orderId=" + orderid;
    }

    wx.navigateTo({
      url: url
    })
  }

})